
var models = require('../db');
var express = require('express');
var router = express.Router();
var mysql = require('mysql');
var $sql = require('../sqlMap');

// 连接数据库
var conn = mysql.createConnection(models.mysql);

conn.connect();
var jsonWrite = function(res, ret) {
    if(typeof ret === 'undefined') {
        res.json({
            code: '1',
            msg: '操作失败'
        });
    } else {
        res.json(ret);
    }
};

// 增加用户接口
router.post('/addUser', (req, res) => {
    var sql = $sql.user.add;
    var params = req.body;
    console.log(params);
    conn.query(sql, [params.name,params.phone,params.password], function(err, result) {
        if (err) {
            console.log(err);
        }
        if (result) {
            jsonWrite(res, result);
        }
    })
});
//查找用户信息接口
router.post('/selectUser', (req,res) => {
    var sql_name = $sql.user.select_name;
    var params = req.body;
    console.log(params);
    conn.query(sql_name,params.username,function(err, result) {
        if(err) {
            console.log(err)
        }
        if(result[0]===undefined) {
            res.send('-1')    //查询不出username，data返回-1
        }else {
            //服务器 Set-Cookie 设置过期时间7天
            // res.cookie('isAuth',1, {maxAge: 7 * 24 * 60 * 1000})
            jsonWrite(res, result);
        }
    });
});
//查询诗歌
router.post('/select_theThink', (req,res) => {
    var select_theThink = $sql.user.select_theThink;
    var params = req.body;
    console.log(params);
    conn.query(select_theThink,function(err, result) {
        if(err) {
            console.log(err)
        }
        if(result[0]===undefined) {
            res.send('-1')    //查询不出username，data返回-1
        }else {
            //服务器 Set-Cookie 设置过期时间7天
            // res.cookie('isAuth',1, {maxAge: 7 * 24 * 60 * 1000})
            jsonWrite(res, result);
        }
    });
});
//查询诗歌
router.post('/select_theWant', (req,res) => {
    var select_theWant = $sql.user.select_theWant;
    var params = req.body;
    console.log(params);
    conn.query(select_theWant,function(err, result) {
        if(err) {
            console.log(err)
        }
        if(result[0]===undefined) {
            res.send('-1')    //查询不出username，data返回-1
        }else {
            //服务器 Set-Cookie 设置过期时间7天
            // res.cookie('isAuth',1, {maxAge: 7 * 24 * 60 * 1000})
            jsonWrite(res, result);
        }
    });
});
//查询诗歌
router.post('/select_theLook', (req,res) => {
    var select_theLook = $sql.user.select_theLook;
    var params = req.body;
    console.log(params);
    conn.query(select_theLook,function(err, result) {
        if(err) {
            console.log(err)
        }
        if(result[0]===undefined) {
            res.send('-1')    //查询不出username，data返回-1
        }else {
            //服务器 Set-Cookie 设置过期时间7天
            // res.cookie('isAuth',1, {maxAge: 7 * 24 * 60 * 1000})
            jsonWrite(res, result);
        }
    });
});
// 增加诗歌到theThink
router.post('/theThink_add', (req, res) => {
    var theThink_add = $sql.user.theThink_add;
    var params = req.body;
    console.log(params);
    conn.query(theThink_add, [params.name,params.author,params.First,params.Second,params.Third,params.Fourth,params.Img,params.Parsing], function(err, result) {
        if (err) {
            console.log(err);
        }
        if (result) {
            jsonWrite(res, result);
        }
    })
});
// 增加诗歌到theLook
router.post('/theLook_add', (req, res) => {
    var theLook_add = $sql.user.theLook_add;
    var params = req.body;
    console.log(params);
    conn.query(theLook_add, [params.name,params.author,params.First,params.Second,params.Third,params.Fourth,params.Img,params.Parsing], function(err, result) {
        if (err) {
            console.log(err);
        }
        if (result) {
            jsonWrite(res, result);
        }
    })
});
// 增加诗歌到theWant
router.post('/theWant_add', (req, res) => {
    var theWant_add = $sql.user.theWant_add;
    var params = req.body;
    console.log(params);
    conn.query(theWant_add, [params.name,params.author,params.First,params.Second,params.Third,params.Fourth,params.Img,params.Parsing], function(err, result) {
        if (err) {
            console.log(err);
        }
        if (result) {
            jsonWrite(res, result);
        }
    })
});
//查询图片
router.post('/select_theLife', (req,res) => {
    var select_theLife = $sql.user.select_theLife;
    var params = req.body;
    console.log(params);
    conn.query(select_theLife,function(err, result) {
        if(err) {
            console.log(err)
        }
        if(result[0]===undefined) {
            res.send('-1')    //查询不出username，data返回-1
        }else {
            //服务器 Set-Cookie 设置过期时间7天
            // res.cookie('isAuth',1, {maxAge: 7 * 24 * 60 * 1000})
            jsonWrite(res, result);
        }
    });
});
//查询更多界面的数据
router.post('/select_more', (req,res) => {
    var select_more = $sql.user.select_more;
    var params = req.body;
    console.log(params);
    conn.query(select_more,function(err, result) {
        if(err) {
            console.log(err)
        }
        if(result[0]===undefined) {
            res.send('-1')    //查询不出username，data返回-1
        }else {
            //服务器 Set-Cookie 设置过期时间7天
            // res.cookie('isAuth',1, {maxAge: 7 * 24 * 60 * 1000})
            jsonWrite(res, result);
        }
    });
});
//查询vlogo界面的数据
router.post('/select_vlogo', (req,res) => {
    var select_vlogo = $sql.user.select_vlogo;
    var params = req.body;
    console.log(params);
    conn.query(select_vlogo,function(err, result) {
        if(err) {
            console.log(err)
        }
        if(result[0]===undefined) {
            res.send('-1')    //查询不出username，data返回-1
        }else {
            //服务器 Set-Cookie 设置过期时间7天
            // res.cookie('isAuth',1, {maxAge: 7 * 24 * 60 * 1000})
            jsonWrite(res, result);
        }
    });
});
module.exports = router;